﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcDeviceManager.Service;
using MvcDeviceManager.ViewModels;
using MvcDeviceManager.Models;

namespace MvcDeviceManager.Controllers
{
    public class DeviceTypeController : Controller
    {
        DeviceTypeService deviceTypeService=new DeviceTypeService();
        //
        // GET: /DeviceType/

        public ActionResult Index()
        {
           IList<DeviceTypeView> list= deviceTypeService.selectAllDeviceType();
           return View(list);
        }

        //
        // GET: /DeviceType/Details/5

        public ActionResult Details(string id)
        {
            DeviceType deviceType = deviceTypeService.GetDeviceTypeByDeviceTypeId(id);
            return View(deviceType);
        }

        //
        // GET: /DeviceType/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /DeviceType/Create

        [HttpPost]
        public ActionResult Create(DeviceType deviceType)
        {
            try
            {

                deviceTypeService.AddDeviceType(deviceType);
                return RedirectToAction("Index");
            }
            catch(Exception e)
            {
                ModelState.AddModelError("", e.Message);
                return View();
            }
        }
        
        //
        // GET: /DeviceType/Edit/5
 
        public ActionResult Edit(string id)
        {
           DeviceType deviceType= deviceTypeService.GetDeviceTypeByDeviceTypeId(id);
           return View(deviceType);
        }

        //
        // POST: /DeviceType/Edit/5

        [HttpPost]
        public ActionResult Edit(DeviceType deviceType)
        {
            try
            {
                //DeviceType deviceType = new DeviceType();
                //deviceType.DeviceTypeId = id;
                //deviceType.DeviceTypeName = collection["DeviceTypeName"];
                deviceTypeService.UpdateDeviceType(deviceType);
                return RedirectToAction("Index");
            }
            catch
            {
                return View(deviceType);
            }
        }

        //
        // GET: /DeviceType/Delete/5

        public ActionResult Delete(string id)
        {
            DeviceType deviceType = deviceTypeService.GetDeviceTypeByDeviceTypeId(id);
            return View(deviceType);
        }

        //
        // POST: /DeviceType/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            try
            {
                deviceTypeService.deleteDeviceTypeByDeviceTypeId(id);
                return RedirectToAction("Index");
            }
            catch(Exception e)
            {
                ModelState.AddModelError("",e.Message);
                DeviceType deviceType = deviceTypeService.GetDeviceTypeByDeviceTypeId(id);
                return View(deviceType);
            }
        }
    }
}
